<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

function smarty_function_innova_get_themes($params, &$smarty)
{
      $themeFolder = "./themes";
      $listThemes = readFolderName($themeFolder);
      $smarty->assign('themes', $listThemes);

}

function readFolderName($dir = "./", $level =1){
  $listDir = array(); 
  if($handler = opendir($dir)) {
      $i = 0; 
      $fileType = array("png");        
      while (($sub = readdir($handler)) !== FALSE) { 
          if ($sub != "." && $sub != ".." && $sub != "Thumb.db") { 
              if(is_dir($dir."/".$sub)){ 
                  $listDir[$i]['themes_name'] = $sub;
                  $listDir[$i]['themes_preview'] = scanFileNameByAllowFileType($dir."/".$sub."/images", $fileType);
                  $i++;    
              } 
          } 
      }    
      closedir($handler); 
  } 
  return $listDir;  
}

function scanFileNameByAllowFileType($rootDir, $allowext, $allData=array()) {
    $dirContent = scandir($rootDir);
    $i = 0;
    foreach($dirContent as $key => $content) {
        $path = $rootDir.'/'.$content;
        $ext = substr($content, strrpos($content, '.') + 1);
        
        if(in_array($ext, $allowext)) {
            if(is_file($path) && is_readable($path)) {
                $allData[$i] = $content;
                $i++;                
            }elseif(is_dir($path) && is_readable($path)) {
                // recursive callback to open new directory
                $allData = scanFileNameByAllowFileType($path, $allData);
            }
        }
    }
    return $allData;
}

?>
